var Data = {};
var ScreenshotTime = {
		centerPicTime : 0,
		lookRatePicTime : 0,
		lookSalesPicTime : 0,
		bottomPicTime : 0,
};

app.controller('doingTakeCtrl', function ($scope) {
	$.base64.utf8decode = true;
	var taskStr = $.base64.decode(Utils.getUrlParam('taskObj'));
	var taskObj = $.parseJSON(taskStr);
	
	Data.taskObj = taskObj;
	$scope.taskObj = taskObj;
	
	if(Data.taskObj.refundType == 2) { //商家立返单提醒
		Utils.msgDialog('<span style="color:#FA4003; font-size:20px; font-weight:bold;">【重要提醒】商家返款任务：</span><BR>'
				+'<span style="color:#FA4003; font-size:18px; font-weight:bold;">● 不能刷虚拟商品<BR>'
				+'● 不能刷无7天理由退换货的商品</span>');
	} else if(Data.taskObj.refundType == 4) { //远程单提醒
		Utils.msgDialog('<span style="color:#FA4003; font-size:20px; font-weight:bold;">【重要提醒】远程付款任务：</span><BR>'
				+'<span style="color:#FA4003; font-size:17px; font-weight:bold;">● 切勿自己垫付<BR>'
				+'● 下订单后立即QQ联系商家进行远程付款<BR>● 远程操作时请勿让对方乱动自己电脑</span>');
	}
	
	if(Data.taskObj.phoneOrder == 1) { //手机单提醒
		$(".bz-title").text("处理手机任务");
		Utils.msgDialog('<span style="color:#FA4003; font-size:18px; font-weight:bold;">请用手机、Pad等无线设备处理此任务<BR>'
				+ '（先加商家QQ'+Data.taskObj.qq+'为好友再做任务）</span>');
		var xybButton = $("#xybButton");
		countDown(xybButton[0], 600); //600秒后才可以点击下一步
	}
	
	//标题生成
	if(Data.taskObj.orderType == 1) {
		if(Data.taskObj.refundType == 1 || Data.taskObj.refundType == 2 || Data.taskObj.refundType == 3) {
			$(".bz-title").append("[垫付单："+ Data.taskObj.taskId +"]");
		} else if(Data.taskObj.refundType == 4) {
			$(".bz-title").append("[远程单："+ Data.taskObj.taskId +"]");
		}
		$(".bz-title").append("["+ Data.taskObj.totalMoney +"元(￥"+ Data.taskObj.price +"x"+ Data.taskObj.amount +"件)]");
	} else if(Data.taskObj.orderType == 2) {
		$(".bz-title").append("[访客单："+ Data.taskObj.taskId +"]");
	}
	var tempRank = "等级无要求";
	if(Data.taskObj.rankLimit == 1) {
		tempRank = "不低于2心";
	} else if(Data.taskObj.rankLimit == 2) {
		tempRank = "不低于4心";
	} else if(Data.taskObj.rankLimit == 3) {
		tempRank = "不低于1钻";
	}
	$(".bz-title").append("["+ Data.taskObj.buyerNick +"("+tempRank+")]");
	
	$scope.showSearchType = {
		"s_type_1": ShowSearchType(1, taskObj.searchType),
		"s_type_2": ShowSearchType(2, taskObj.searchType),
		"s_type_3": ShowSearchType(3, taskObj.searchType),
		"s_type_4": ShowSearchType(4, taskObj.searchType),
		"s_type_5": ShowSearchType(5, taskObj.searchType),
		"_s_type_5": !ShowSearchType(5, taskObj.searchType),
	};
	
	$scope.showCompare = DIC.ShowCompare(taskObj.compare, 1);	//是否展示货比三家
	$scope.compareOne = DIC.ShowCompare(taskObj.compare, 1);
	$scope.compareTwo = DIC.ShowCompare(taskObj.compare, 2);
	$scope.compareThree = DIC.ShowCompare(taskObj.compare, 3);
	
	$scope.showTalk = DIC.TrueFalse(taskObj.talk);
	$scope.showCollect = DIC.TrueFalse(taskObj.collect);
	$scope.showCollectShop = DIC.TrueFalse(taskObj.collectShop);
	$scope.showLookRate = DIC.TrueFalse(taskObj.lookRate);
	$scope.showLookSales = DIC.TrueFalse(taskObj.lookSales);
	$scope.showBrowseCenter = DIC.TrueFalse(taskObj.browseCenter);
	$scope.showBrowseFoot = DIC.TrueFalse(taskObj.browseFoot);
	
	$scope.showBrowseOthers = DIC.ShowCompare(taskObj.browseOthers, 1);	//是否展示店铺宝贝浏览
	$scope.browseOthersOne = DIC.ShowCompare(taskObj.browseOthers, 1);
	$scope.browseOthersTwo = DIC.ShowCompare(taskObj.browseOthers, 2);
	$scope.browseOthersThree = DIC.ShowCompare(taskObj.browseOthers, 3);
	
}); //app.controller结束



$(document).ready(function() {
	
	/*
	初始化步骤编号
	*/
	$(".step-no").each(function(index, element) {
		$(element).text(index+1);
	});
	
    Utils.appendZclip($("#look-pro-link")[0], $("#pro-link-val")[0]);	//查看宝贝链接
	Utils.appendZclip($("#tb-index-link")[0], $('<input type="hidden" value="http://www.taobao.com/"/>')[0]);	//查看淘宝首页链接
	Utils.appendZclip($("#tb-index-link")[1], $('<input type="hidden" value="http://www.taobao.com/"/>')[0]);	//查看淘宝首页链接
	Utils.appendZclip($("#ju-index-link")[0],$('<input type="hidden" value="http://ju.taobao.com/"/>')[0]);	//查看聚划算首页链接
	Utils.appendZclip($("#te-index-link")[0], $('<input type="hidden" value="http://tejia.taobao.com/"/>')[0]);	//查看天天特价首页链接
	
	//链接任务：扫二维码
	$("#pro-link-ma").click(function() {
		var proLink = $("#pro-link-val").val();
		var content = $('<div id="qrcode"><BR><span style="color:#F03100;">&nbsp;&nbsp;宝贝链接二维码</span></div>');
		content.qrcode({
			text: proLink,
			width: 150,
			height: 150,
			minVersion: 1,
			render: "DIV",
			ecLevel: "L",
		});
		Utils.msgDialog(content);
    });
	
	/*
	验证宝贝链接
	*/
	$("#check-pro-link").click(function() {
		if(Data.taskObj.phoneOrder == 0) {	//电脑单
			var params = getParams();
			if(Data.taskObj.searchType != 5 && Data.taskObj.compare == 1) {
				if(params.compare1Pic.length < 10) {
					Utils.msgDialog("请上传“货比三家”截图（1家）！");
					return;
				}
			} else if(Data.taskObj.searchType != 5 && Data.taskObj.compare == 2) {
				if(params.compare1Pic.length < 10 || params.compare2Pic.length < 10) {
					Utils.msgDialog("请上传“货比三家”截图（2家）！");
					return;
				}
			} else if(Data.taskObj.searchType != 5 && Data.taskObj.compare == 3) {
				if(params.compare1Pic.length < 10 || params.compare2Pic.length < 10 || 
														params.compare3Pic.length < 10) {
					Utils.msgDialog("请上传“货比三家”截图（3家）！");
					return;
				}
			}
		}
		
		var taskId = Data.taskObj.taskId;
		var productLink = $.trim($("#in-pro-link").val());
		if(productLink.length < 10) {
			Utils.msgDialog("请输入有效的宝贝链接！");
			return ;
		}
		
		var params = {
			"taskId": taskId,
			"productLink": productLink,
		};
		AQ.callAQ("checkProductLink.do", params,
			function(result) {
				Utils.msgDialog("验证通过");
				$("#in-pro-link").prop('disabled', true);
				$("#check-pro-link").hide();
				countDown($("#xybButton")[0], 60 * 3); 	//60 * Data.taskObj.stayTime
			},
			function(err, code){
				if(code == 8) {
					Utils.msgDialog("<span style='color:#FF4100; font-weight:bold;'>宝贝链接不正确！</span><BR>"+
						"请按照发布方给出的提示再找找吧！也可以QQ联系发布方求助哦！");
				} else {
					Utils.msgDialog(err+"[code:"+code+"]");
				}
			}
		);
    });
	
    /*
	点击下一步弹出对话框
	*/
	$("#xybButton").click(function() {
		var orderType = Data.taskObj.orderType;
		if(orderType == 1) {	//销量单
			submitData();
		} else if(orderType == 2) {	//访客单
			var params = getParams();
			if(!checkParams(params)) {
				return;
			}
			Data.params = params;
			payDialog("访客单：确认完成任务");
		}
    });
	
	
});	//$(document).ready结束


/*
显示确认付款对话框

@param title	对话框标题
*/
function payDialog(title) {
	$("#payDialog").each(function(index, element) {
        $(element).remove();
    });
	var content = $('<div id="payDialog"></div>').load("doing-pay-dialog.html");
	var d = dialog({
			fixed: true,
			zIndex: 999999999,
    		title: title,
  	  		content: content,
    		cancel: false,
			okValue: '&nbsp;返&nbsp;回&nbsp;',
    		ok: function () {
				//销量单：在处理任务页面时，关闭当前页
				if(window.location.href.indexOf("doing-take.html") >= 0 && Data.taskObj.orderType == 1) {
					Utils.closeCurrentPage();
				}
			}
	});
	d.width(450);
	d.height(350);
	d.showModal();
}

/*
判断找宝贝方式
*/
function ShowSearchType(no, searchType) {
	if(no == searchType) {
		if(searchType == 5) { //链接任务
			countDown($("#xybButton")[0], 60 * 3); 	//60 * Data.taskObj.stayTime
		}
		return true;
	}
	return false;
};

/**
获取参数
*/
function getParams() {
	var orderType = Data.taskObj.orderType;
	var taskId = Data.taskObj.taskId;
	var productLink;
	if(Data.taskObj.searchType == 5) {
		productLink = $("#pro-link-val").val();
	} else {
		productLink = $.trim($("#in-pro-link").val());
	}
	var compare1Pic = $("#imgUrl_doing-comp-1").val();
	var compare2Pic = $("#imgUrl_doing-comp-2").val();
	var compare3Pic = $("#imgUrl_doing-comp-3").val();
	var browseCenterPic = $("#imgUrl_doing-browseCenter").val();
	var lookRatePic = $("#imgUrl_doing-lookRate").val();
	var lookSalesPic = $("#imgUrl_doing-lookSales").val();
	var collectPic = $("#imgUrl_doing-collect").val();
	var browse1Pic = $("#imgUrl_doing-brow-1").val();
	var browse2Pic = $("#imgUrl_doing-brow-2").val();
	var browse3Pic = $("#imgUrl_doing-brow-3").val();
	var collectShopPic = $("#imgUrl_doing-collectShop").val();
	var talkPic = $("#imgUrl_doing-talk").val();
	
	var params = {
		orderType: orderType,
		taskId: taskId,
		productLink: productLink,
		compare1Pic: compare1Pic,
		compare2Pic: compare2Pic,
		compare3Pic: compare3Pic,
		centerPic: browseCenterPic,
		lookRatePic: lookRatePic,
		lookSalesPic: lookSalesPic,
		collectPic: collectPic,
		browse1Pic: browse1Pic,
		browse2Pic: browse2Pic,
		browse3Pic: browse3Pic,
		collectShopPic: collectShopPic,
		talkPic: talkPic,
	};
	return params;
}

/*
检测参数
*/
function checkParams(params) {
	if(Data.taskObj.phoneOrder == 0 && Data.taskObj.searchType != 5 && !$("#in-pro-link").prop('disabled')) {
		Utils.msgDialog("请验证宝贝链接！");
		return false;
	}
	
	if(Data.taskObj.phoneOrder == 0) {	//电脑单
		if(Data.taskObj.searchType != 5 && Data.taskObj.compare == 1) {
			if(params.compare1Pic.length < 10) {
				Utils.msgDialog("请上传“货比三家”截图（1家）！");
				return false;
			}
		} else if(Data.taskObj.searchType != 5 && Data.taskObj.compare == 2) {
			if(params.compare1Pic.length < 10 || params.compare2Pic.length < 10) {
				Utils.msgDialog("请上传“货比三家”截图（2家）！");
				return false;
			}
		} else if(Data.taskObj.searchType != 5 && Data.taskObj.compare == 3) {
			if(params.compare1Pic.length < 10 || params.compare2Pic.length < 10 || 
													params.compare3Pic.length < 10) {
				Utils.msgDialog("请上传“货比三家”截图（3家）！");
				return false;
			}
		}
		if(Data.taskObj.browseCenter == 1 && params.centerPic.length < 10) {
			Utils.msgDialog("请上传“宝贝详情中部”截图！");
			return false;
		}
		if(Data.taskObj.lookRate == 1 && params.lookRatePic.length < 10) {
			Utils.msgDialog("请上传“宝贝评论”截图！");
			return false;
		}
		if(Data.taskObj.lookSales == 1 && params.lookSalesPic.length < 10) {
			Utils.msgDialog("请上传“宝贝成交记录”截图！");
			return false;
		}
		if(Data.taskObj.collect == 1 && params.collectPic.length < 10) {
			Utils.msgDialog("请上传“宝贝收藏成功”截图！");
			return false;
		}
		if(Data.taskObj.browseOthers == 1) {
			if(params.browse1Pic.length < 10) {
				Utils.msgDialog("请上传“浏览店铺宝贝”截图（1个）！");
				return false;
			}
		} else if(Data.taskObj.browseOthers == 2) {
			if(params.browse1Pic.length < 10 || params.browse2Pic.length < 10) {
				Utils.msgDialog("请上传“浏览店铺宝贝”截图（2个）！");
				return false;
			}
		} else if(Data.taskObj.browseOthers == 3) {
			if(params.browse1Pic.length < 10 || params.browse2Pic.length < 10 || 
													params.browse3Pic.length < 10) {
				Utils.msgDialog("请上传“浏览店铺宝贝”截图（3个）！");
				return false;
			}
		}
		if(Data.taskObj.collectShop == 1 && params.collectShopPic.length < 10) {
			Utils.msgDialog("请上传“店铺收藏成功”截图！");
			return false;
		}
		if(Data.taskObj.talk == 1 && params.talkPic.length < 10) {
			Utils.msgDialog("请上传“旺旺聊天”截图！");
			return false;
		}
	}
	return true;
}

/*
销量单：点击下一步（提交数据）
*/
function submitData() {
	var params = getParams();
	if(!checkParams(params)) {
		return;
	}
	
	if(Data.taskObj.sameShopTakedNum >= 2) {	//同店铺重复接单改地址提醒
		var content = '<span style="font-size:16px; font-weight:bold; color:#F52B00;">' +
				'您15天内已经在该店铺刷过'+ (Data.taskObj.sameShopTakedNum - 1) +'单了，为了您的买号安全，请务必不要使用跟原来一样的收货地址。<BR>' +
				'（空包一般都不用签收，收货地址可稍作修改。）</span>';
		sameShopTips(params, content);
	} else  {
		AQ.callAQ("browsePro.do", params, function(result) {
			$("#xybButton").css("background", "#DDDDDD");
			$("#xybButton").prop("disabled", true);
			var title = "温馨提示";
			if(Data.taskObj.refundType == 1 || Data.taskObj.refundType == 2 || Data.taskObj.refundType == 3) {
				title = "垫付单：确认付款";
			} else if(Data.taskObj.refundType == 4) {
				title = "远程单：确认下订单";
			}
			payDialog(title);
		}, function(err, code){
			Utils.msgDialog(err+"[code:"+code+"]");
		});
	}
}

/**
 * 同店铺接单改地址提醒
 * 
 * @param params
 * @param content
 */
function sameShopTips(params, content) {
	var d = dialog({
			fixed: true,
			zIndex: 999999999,
    		title: '同店铺重复接单改地址提醒',
  	  		content: content,
    		cancel: false,
			okValue: '我知道了',
    		ok: function () {
    			AQ.callAQ("browsePro.do", params, function(result) {
    				$("#xybButton").css("background", "#DDDDDD");
    				$("#xybButton").prop("disabled", true);
    				var title = "温馨提示";
    				if(Data.taskObj.refundType == 1 || Data.taskObj.refundType == 2 || Data.taskObj.refundType == 3) {
    					title = "垫付单：确认付款";
    				} else if(Data.taskObj.refundType == 4) {
    					title = "远程单：确认下订单";
    				}
    				payDialog(title);
    			}, function(err, code){
    				Utils.msgDialog(err+"[code:"+code+"]");
    			});
			}
	});
	d.width(550);
	d.height(80);
	d.showModal();
}

